//
//  WBVisitorView.swift
//  XtWeiBo
//
//  Created by 徐腾 on 2017/7/24.
//  Copyright © 2017年 徐腾. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {

    /// 注册按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton("登录",
                                                                        fontSize: 14,
                                                                        normalColor: UIColor.darkGray,
                                                                        highlightedColor: UIColor.black,
                                                                        backgroundImageName: "common_button_white_disable")
    /// 登录按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton("注册",
                                                                           fontSize: 14,
                                                                           normalColor: UIColor.orange,
                                                                           highlightedColor: UIColor.black,
                                                                           backgroundImageName: "common_button_white_disable")
    
    // 设置访客视图 图片和标识文字 的字典
    var visitorDict: [String: String]? {
        didSet {
            
            guard let imageName = visitorDict?["iconImageName"],
                  let message = visitorDict?["message"]
            
                else {
                return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                // 开始动画
                startAnimation()
                return
            }
            
            iconImageView.image = UIImage(named: imageName)
            
            // 其他控制器的访客视图的小房子和遮罩图层就不需要显示了
            houseIconImageView.isHidden = true
            maskImageView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 懒加载控件
    /// 背景图片
    fileprivate lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图片
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    fileprivate lazy var houseIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 描述label
    fileprivate lazy var tipLabel: UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
    
}

extension WBVisitorView {
 
    
    /// 添加动画
    fileprivate func startAnimation() {
        
        let anni = CABasicAnimation(keyPath: "transform.rotation")
        
        anni.toValue = 2 * Double.pi
        anni.repeatCount = MAXFLOAT
        anni.duration = 20
        
        // 当iconImageView释放的时候 动画也会自动释放
        anni.isRemovedOnCompletion = false
        
        iconImageView.layer.add(anni, forKey: nil)
    }
    
    
    /// 设置访客视图的界面
    fileprivate func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(houseIconImageView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 关闭 autoresizing   xib使用 autolayout  纯代码使用 autoresizing
        for i in subviews {
            i.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置访客视图的标识 文字居中
        tipLabel.textAlignment = .center
        
        //添加约束
        /// 1> icon 图标
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: -60))
        
        /// 2> 小房子
        addConstraint(NSLayoutConstraint(item: houseIconImageView, attribute: .centerX, relatedBy: .equal, toItem: iconImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconImageView, attribute: .centerY, relatedBy: .equal, toItem: iconImageView, attribute: .centerY, multiplier: 1, constant: 0))
        
        /// 3> 提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: iconImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: iconImageView, attribute: .bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 236))
        
        /// 4>注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        
        /// 5>登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: registerButton, attribute: .width, multiplier: 1, constant: 0))
        
        /// 6>设置遮罩图像
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .bottom, relatedBy: .equal, toItem: registerButton, attribute: .bottom, multiplier: 1, constant: 0))
    }
    
}


